/**
 * 
 */
package com.jolestar.yayaweibotong.service.impl;

import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.oauth.client.HttpClientPool;
import net.oauth.client.OAuthClient;
import net.oauth.client.OAuthHttpClient;
import net.oauth.client.OAuthURLConnectionClient;

import org.apache.commons.httpclient.HttpClient;

import com.google.gdata.client.douban.DoubanService;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.TextContent;
import com.google.gdata.data.douban.MiniblogEntry;
import com.google.gdata.data.douban.MiniblogFeed;
import com.jolestar.yayaweibotong.ServiceType;
import com.jolestar.yayaweibotong.YaYaConstants;
import com.jolestar.yayaweibotong.YayaException;
import com.jolestar.yayaweibotong.dao.AccountDomain;
import com.jolestar.yayaweibotong.dao.StatusDomain;

/**
 * @author jolestar@gmail.com
 * 
 */
public class DoubanStatusService extends AbstractStatusService<DoubanService> {

	public static final OAuthClient OAUTH_CLIENT = new OAuthURLConnectionClient();

	static{
		DoubanService.CLIENT = OAUTH_CLIENT;
	}
	@Override
	public List<StatusDomain> getLatestStatuses(AccountDomain account,
			String sinceId) {
		DoubanService service = this.getOrCreateInstance(account);
		List<StatusDomain> results = new ArrayList<StatusDomain>();
		try {
			MiniblogFeed feed = service.getUserMiniblogs(
					account.getOriginalId(), 1, 20);
			List<MiniblogEntry> entries = feed.getEntries();
			for (MiniblogEntry entry : entries) {
				if (sinceId != null && entry.getId().equals(sinceId)) {
					break;
				}
				StatusDomain status = this.entryToDomain(entry, account);
				results.add(status);
			}
		} catch (Exception e) {
			throw new YayaException(e);
		}
		return results;
	}
	
	private StatusDomain entryToDomain(MiniblogEntry entry,AccountDomain account){
		StatusDomain status = new StatusDomain(ServiceType.douban,
				entry.getId(), account);
		status.setText(((TextContent) entry.getContent()).getContent()
				.getPlainText());
		status.setCreateAt(new Date(entry.getPublished().getValue()));
		status.save();
		return status;
	}

	@Override
	public void updateAccount(AccountDomain account) {
		if (account.getName() == null) {
			account.setName(account.getOriginalId());
			account.save();
		}
	}

	@Override
	public StatusDomain updateStatus(AccountDomain account, StatusDomain status) {
		DoubanService service = this.getOrCreateInstance(account);
		// service.getUserMiniblogs(userId, startIndex, maxResult)
		try {
			MiniblogEntry entry = service.createSaying(new PlainTextConstruct(status
					.formatStatus(ServiceType.douban.getStatusLengthLimit())));
			StatusDomain result = this.entryToDomain(entry, account);
			result.setSynced(true);
			result.setSyncedAt(new Date());
			return result;
		} catch (Exception e) {
			throw new YayaException(e);
		}
	}

	@Override
	protected DoubanService createInstance(AccountDomain account) {
		DoubanService service = new DoubanService(YaYaConstants.APP_NAME,
				ServiceType.douban.getApiKey(),
				ServiceType.douban.getApiSecret());
		service.setAccessToken(account.getToken(), account.getTokenSecret());
		return service;
	}

}
